package com.azt.front.action.company;

import com.azt.api.dto.member.MemberExt;
import com.azt.api.dto.member.MemberParam;
import com.azt.api.pojo.Category;
import com.azt.api.service.MemberService;
import com.azt.api.service.ProCategoryService;
import com.azt.api.service.ProductService;
import com.azt.model.page.TPagination;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 集中采购首页
 * Created by 张栋 on 2017/2/1410:28
 */
@Controller
@RequestMapping("/center")
public class CenterPurchaseActioon {


    @Autowired
    private MemberService memberService;


    @Autowired
    private ProductService productService;


    @Autowired
    private ProCategoryService proCategoryService;


    /**
     * 集中采购首页
     * Created by 张栋 on 2017/2/14 10:29
     */
    @RequestMapping("")
    public String centerIndex(ModelMap model, MemberParam param) {

        String categoryIds = param.getCategoryIds();
        if (StringUtils.isNotBlank(categoryIds)) {
            try {
                Integer.parseInt(categoryIds);
            } catch (NumberFormatException e) {
                categoryIds = "";
            }
        }
        param.setCategoryIds(categoryIds);

        model.put("param",param);
        return "shop/center_index";
    }



    /**
     * 异步检索商品
     * Created by 张栋 on 2017/2/17 13:25
     */
    @RequestMapping("center_search")
    @ResponseBody
    public TPagination offerSearch(@RequestBody MemberParam param){
        Integer catid =0;
        try {
            String categoryIds = param.getCategoryIds();
            catid = Integer.parseInt(categoryIds);
        } catch (NumberFormatException e) {

        }

        param.setPageSize(16);
        //获取检索到的产品
        TPagination<MemberExt> memberExt = memberService.getMembersExtByParam(param);

        //只查一级
        List<Category> childenCategorys = proCategoryService.getCacheCategoryByPid(0,false,false);
        memberExt.setParam("categoryList",childenCategorys);
        memberExt.setParam("current",catid);
        return memberExt;
    }
}
